<template>
  <div id="userLogin">
    <h2 style="margin-bottom: 16px">用户登录</h2>
    <a-form
      :model="form"
      label-align="left"
      auto-label-width
      @submit="handleSubmit"
      class="loginForm"
    >
      <a-form-item field="userAccount" label="账号">
        <a-input v-model="form.userAccount" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item field="userPassword" tooltip="密码不少于8位" label="密码">
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码"
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 120px"
          >登录
        </a-button>
        <router-link to="#" class="register-link" style="margin-left: 80px"
          >忘记密码
        </router-link>
        <router-link
          to="/user/register"
          class="register-link"
          style="margin-left: 90px"
          >注册
        </router-link>
      </a-form-item>
    </a-form>
  </div>
</template>
<style scoped>
#userLogin {
  margin-top: 80px;
}

#userLogin .loginForm {
  max-width: 480px;
  margin: 0 auto;
}

#userLogin .register-link {
  color: blue;
  text-decoration: none;
  /* margin-top: 10px; */
  font-size: 18px;
}
</style>

<script setup lang="ts">
import { reactive } from "vue";
import { UserControllerService, UserLoginRequest } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import { useStore } from "vuex";

const router = useRouter();
const store = useStore();
/**
 * 表单数据
 */
const form = reactive({
  userAccount: "",
  userPassword: "",
} as UserLoginRequest);
/**
 * 提交表单
 */
const handleSubmit = async () => {
  const res = await UserControllerService.userLoginUsingPost(form); //将用户信息发送给后台
  if (res.code === 0) {
    //alert("登录成功" + JSON.stringify(res.data));
    //修改当前用户的状态信息,去后端请求登录用户信息
    await store.dispatch("user/getLoginUser");
    //获取到登录用户信息后，返回主页，此时成功显示用户信息
    router.push({
      path: "/",
      replace: true,
    });
  } else {
    message.error("登录失败," + res.message);
  }
};
</script>
